<?php

	// +----------------------------------------------------------------------
	// | 用户中心公开控制器
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\controller\ucenter;

	use app\event\cms\Document as DocumentEvent;
	use app\common\controller\Base;
	use app\common\middleware\UserAuth;
	use app\event\ucenter\Notice as NoticeEvent;
	use app\event\ucenter\User as UserEvent;
	use app\model\ucenter\EmailHistory;
	use app\model\ucenter\User as UserModel;
	use app\event\ucenter\Action as ActionEvent;
	use think\Container;
	use think\facade\Cookie;
	use think\facade\Log;
	use think\facade\Url;
	use think\facade\Config;
	use think\facade\Session;
	use think\facade\Lang;

	class Foreign extends Base
	{

		/**
		 * 当前登录用户
		 * @var UserModel
		 */
		protected $user;


		protected function initialize()
		{
			parent::initialize();
			$this->user = $this->request->user;
			$this->assign('user', $this->user);
			$this->assign('extend', 'index');
			$this->assign('version', '3.0.13');
		}

		/**
		 * 退出登陆.
		 */
		public function logout()
		{

			$module = $this->request->param('m');

			$user = UserEvent::getLogin($module);

			if ($user) {
				UserEvent::logout($module);
				$data['code'] = 0;
                if ($module == 'admin')
                {
                    $data['url'] = (string)url('/');
                }else{
                    $data['url'] = (string)url('/ucenter/login/' . $this->request->param('m'));
                }
				$data['message'] = '退出成功';

				// 主动退出的情况下不再自动登陆
				Cookie::delete('auto_login');
				//                Cookie::set('login_redirect', $this->request->server('HTTP_REFERER'));
			} else {
				$data['code'] = 1;
				$data['message'] = '用户未登录';
			}
			if ($this->request->isAjax()) {
				return json($data);
			} else {
				if (1 == $data['code']) {
					$this->error($data['message']);
				} else {
					$this->redirect($data['url']);
				}
			}
		}

		/**
		 * 用户登陆.
		 * @return mixed|string
		 */
		public function login()
		{

			if ($this->request->isPost()) {
				// 安全验证码
				// 图像验证码
//				$limit = ActionEvent::limit('login');
				if (!$this->request->param('captcha')) {
					$data['code'] = 20001;
					$data['message'] = '请输入验证码';
				} else {
					$validate = $this->validate($this->request->post(), 'app\validate\ucenter\User.login');

					if ( true !== $validate) {
						$data['code'] = 20002;
						$data['message'] = $validate;
					} else {
						$user_event = new UserEvent();
						$data = $user_event->login($this->request->param(), $this->request->param('m'));
					}
				}
				return json($data);
			} else {
				$module = $this->request->param('m');

				if (!UserEvent::getLogin($module)) {
					$this->assign('module', $module);
					$this->assign('mobile', $this->request->isMobile() ? 1 : 0);
//					$limit = ActionEvent::limit('login');
//					if (0 == $limit['code']) {
//						$this->assign('limit', $limit['data']);
//					}
//					Log::record(1);
					if ($module !== 'admin') {
						return $this->fetch($module . '@foreign/login');
					} else {
						return $this->fetch('ucenter/login/green');
//						return $this->fetch('login/' . Config::get('app.login_theme'));
					}

				} else {
                    $this->redirect('/admin');
//					$this->error('用户已经登陆', '/admin');
				}
			}
		}

		public function top()
		{
			$event = new UserEvent();
			$data = $event->top($this->request->param());
			return json($data);
		}

		public function page()
		{
			$event = new DocumentEvent();
			$result = $event->detail($this->request->param());
			if (0 == $result['code']) {
				return $result['data']['article']['content'];
			} else {
				return '暂无更新';
			}
		}

		public function forget()
		{
			// 忘记重置密码
			if ($this->request->isPost()) {

				$validate = $this->validate($this->request->post(), 'app\validate\ucenter\User.forget');
				if (true !== $validate) {
					$data['code'] = 20002;
					$data['message'] = $validate;
				} else {
					$user = new UserEvent();
					$data = $user->forget($this->request->post());
				}

				return json($data);
			} else {
				$module = $this->request->param('m');
				if ($module !== 'admin') {
					$this->assign('module', $module);
					return $this->fetch($module . '@foreign/forget');
				} else {
					return $this->fetch();
				}
			}
		}

		public function agreement()
		{
			return 'ok';
		}

		/**
		 * 用户注册
		 */
		public function register()
		{
			if ($this->request->isPost()) {
				// 安全验证
				// 数据验证
				$result = $this->validate($this->request->post(), 'app\validate\ucenter\User.register');
				if (true !== $result) {
					// 验证失败 输出错误信息
					$data['code'] = 20003;
					$data['message'] = $result;
				} else {
					$user = new UserEvent();
					$data = $user->register($this->request->post());
				}
				return json($data);
			} else {

				if (!UserEvent::getLogin()) {
					$module = $this->request->param('m');
					$this->assign('url', url('ucenter/Foreign/login', ['module' => $module]));
					$this->assign('invite_code', $this->request->param('code', ''));
					if ($module !== 'admin') {
						return $this->fetch($module . '@foreign/register');
					} else {
						return $this->fetch('foreign/register');
					}
				} else {
					$this->error('用户已经登陆');
				}
			}
		}

		/**
		 * 用户激活
		 */
		public function activate()
		{
			if ($this->request->isPost()) {
				// 安全验证
				// 数据验证
				$result = $this->validate($this->request->post(), 'app\validate\ucenter\User.register');
				if (true !== $result) {
					// 验证失败 输出错误信息
					$data['code'] = 20003;
					$data['message'] = $result;
				} else {
					$user = new UserEvent();
					$data = $user->register($this->request->post(), 'activate');
				}
				return json($data);
			} else {

				if (!UserEvent::getLogin()) {
					$module = $this->request->param('m');
					$email = EmailHistory::where('token', $this->request->param('t'))->where('status', 1)->whereTime('create_time', '>', '-1 day')->find();
					if (!$email) {
						$this->error('链接已经失效，请重新获取');
					}
					$this->assign('url', url('ucenter/Foreign/login', ['module' => $module]));
					$this->assign('email', $email['address']);
					$this->assign('activate', $email['token']);
					if ($module !== 'admin') {
						return $this->fetch($module . '@foreign/activate');
					} else {
						return $this->fetch('foreign/activate');
					}
				} else {
					$this->error('用户已经登陆');
				}
			}
		}

		/**
		 * 重置登录密码
		 */
		public function reset()
		{
			if ($this->request->isPost()) {
				// 安全验证

				$user = new UserEvent();
				$data = $user->reset($this->request->post());
				return json($data);
			} else {

				$module = $this->request->param('m');
				$email = EmailHistory::where('token', $this->request->param('t'))->where('status', 1)->whereTime('create_time', '>', '-1 day')->find();
				if (!$email) {
					$this->error('链接已经失效，请重新获取');
				}
				$this->assign('url', url('ucenter/Foreign/login', ['module' => $module]));
				$this->assign('email', $email['address']);
				$this->assign('activate', $email['token']);
				$this->assign('type', $this->request->param('type'));
				if ($module !== 'admin') {
					return $this->fetch($module . '@foreign/reset');
				} else {
					return $this->fetch('foreign/reset');
				}
			}
		}

		/**
		 * 重置登录密码
		 */
		public function reset_email()
		{
			if ($this->request->isPost()) {
				// 安全验证
				$user = new UserEvent();
				$data = $user->email_reset_do($this->request->post());
				return json($data);
			} else {
				$module = $this->request->param('m');
				$email = EmailHistory::where('token', $this->request->param('t'))->where('status', 1)->whereTime('create_time', '>', '-1 day')->find();
				if (empty($email['param']['uid'])) {

					$this->error('链接已经失效，请重新获取');
				}
				$user = UserModel::where('id', $email['param']['uid'])->find();

				if (!$user) {

					$this->error('链接已经失效，请重新获取');
				}

				$this->assign('user', $user);
				$this->assign('url', url('ucenter/Foreign/login', ['module' => $module]));
				$this->assign('email', $email['address']);
				$this->assign('activate', $email['token']);
				return $this->fetch($module . '@foreign/reset_email');

			}
		}

		/**
		 * 发送手机验证码
		 */
		public function sms()
		{
			$validate = $this->validate($this->request->post(), '\app\validate\ucenter\Sms');
			if (true === $validate) {
				$data = NoticeEvent::safeCode($this->request->post(), $this->request->user);
			} else {
				$data['code'] = 1;
				$data['message'] = $validate;
			}
			return json($data);
		}

		public function email()
		{
			$validate = $this->validate($this->request->param(), '\app\validate\ucenter\Email.send');
			if (true === $validate) {
				$data = NoticeEvent::safeCode($this->request->param(), $this->request->user, 'email');
			} else {
				$data['code'] = 1;
				$data['message'] = $validate;
			}
			return json($data);
		}

		public function safe()
		{
			if ($this->request->user) {
				$channel = $this->request->param('safe_check_type', 'mobile');
				$data = $this->request->param();
				$data['email'] = $this->request->user['email'];
				$data['mobile'] = $this->request->user['mobile'];
				$data = NoticeEvent::safeCode($data, $this->request->user, $channel);
			} else {
				$data['code'] = 1;
				$data['message'] = '请先登录';
			}
			return json($data);
		}

		public function code()
		{
			$channel = $this->request->param('safe_check_type', 'mobile');

			if ($channel == 'mobile') {
				$validate = $this->validate($this->request->param(), '\app\validate\ucenter\Sms');
			} else {
				$data = $this->request->param();
				$data['address'] = $data['email'];
				$validate = $this->validate($data, '\app\validate\ucenter\Email.send');
			}
			if (true === $validate) {
				if (isset($data['email'])) {
					$data['address'] = $data['email'];
				}
				$data = NoticeEvent::safeCode($this->request->param(), $this->request->user, $channel);
			} else {
				$data['code'] = 1;
				$data['message'] = $validate;
			}
			return json($data);
		}


	}
